﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Surpass.Model.Dto.RequestParamsDto;
using Surpass.Model.Dto.ResponseDto.ResponseModel.ResultModel;
using Surpass.Repository.IRepository;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Surpass.Model.SysDomain;
using System.IO;
using MiniExcelLibs;

namespace Surpass.Admin.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : BaseController
    {
        private readonly ISysUserRepository _sysUserRepository;
        public UserController(ISysUserRepository sysUserRepository)
        {
            _sysUserRepository = sysUserRepository;
        }

        [HttpGet,Route("page/{pageSize}/{currentPage}")]
        public async Task<IActionResult> GetManagerUser(string uName,string eMail, [Range(1, 100, ErrorMessage = "页大小范围1-100")][FromRoute] int currentPage = 1, [FromRoute]  int pageSize = 10)
        {
            var result = await _sysUserRepository.GetUserList(uName, eMail, currentPage, pageSize);
            return Ok(result);
        }

        [HttpPost,Route("AddUser")]
        public async Task<IActionResult> AddSysUser(SysUserParams sysUserParams)
        {
            var result = await _sysUserRepository.AddSysUser(sysUserParams);
            return Ok(result);
        }
        [HttpPost,Route("EditUser")]
        public IActionResult EditSysUser(SysUserParams sysUserParams)
        {
            var result = _sysUserRepository.EditSysUser(sysUserParams);
            return Ok(result);
        }
        [HttpPost,Route("DeleteUser")]
        public IActionResult DeleteSysUser(SysUserParams sysUserParams)
        {
            var result = _sysUserRepository.DeleteSysUser(sysUserParams);
            return Ok(result);
        }

        /// <summary>
        /// 导入Excel
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost,Route("UpLoad")]
        public IActionResult UpLoad()
        {
            try
            {
                var excel = this.HttpContext.Request.Form.Files[0];
                //实例化 内存流
                var stream = new MemoryStream();
                //将上传文件存放到内存当中
                excel.CopyTo(stream);
                var list = stream.Query<SysUser>().ToList();
                return Ok(_sysUserRepository.BatchAddAsync(list));
            }
            catch (Exception)
            {

               //throw;
            }
            return null;
           
        }

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        [HttpGet,Route("DownloadExcel")]
        public IActionResult DownloadExcel()
        {
            var values = _sysUserRepository.GetUserList("", "", 1, 10000);

            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(values.Result.PageData);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "demo.xlsx"
            };
        }
    }
}
